package com.tl.sboot.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.google.gson.Gson;
import com.tl.sboot.service.StkCodeService;
import com.tl.sboot.utils.FileUtil;

@RestController
public class StkCodeController {
	@Autowired
	StkCodeService stkCodeService;
	
	@RequestMapping(value="/findByParStkCode/{parListJson}",method = RequestMethod.GET)
	public List<Map<String, Object>> findByParStkCode(String parListJson){
		return stkCodeService.findByParStkCode(parListJson);
	}
	
	@RequestMapping(value="/findFieldsWithObjList",method = RequestMethod.GET)
	public void findFieldsWithObjList(String param,HttpServletRequest request,HttpServletResponse response) throws IOException {
		List<Map<String, Object>> maps = stkCodeService.findFieldsWithObjList(param.trim());
		boolean jsonP = false;
		String cb = request.getParameter("callback");
		response.setContentType("text/html; charset=utf-8");
		if (cb != null) {
		    jsonP = true;
		    response.setContentType("text/javascript");
		} else {
		    response.setContentType("application/x-json");
		}
		PrintWriter out = response.getWriter();
		if (jsonP) {
		    out.write(cb + "(");
		}
		Gson gson = new Gson();
		out.print(gson.toJson(maps));
		if (jsonP) {
		    out.write(");");
		}
		
	}
	/**
	 * 得到字符串生成Python文件
	 * @param code
	 */
	@RequestMapping(value="/newPythonFile",method = RequestMethod.POST)
	@ResponseBody
	public String newPythonFile(String code){
		FileUtil fu = new FileUtil();
		String StrategyPath = fu.getFilePath("Strategy.py");
		String DataSourcePath = fu.getFilePath("DataSource.py");
		String strategyText = FileUtil.readTxtFile(StrategyPath);
		String dataSourceText = FileUtil.readTxtFile(DataSourcePath);
		FileUtil.createFile("py",strategyText+code+dataSourceText,"py");
		String readTxtFile = FileUtil.readTxtFile(FileUtil.filenameTemp);
		return readTxtFile;
	}
	
}
